import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class Main extends MIDlet implements CommandListener {

	private static Display display;
	private static Form form;
	static Main midlet;
	Menu menu;
	
	private static final String VIEW_PROFILE = "Visualizar Perfil";
	private static final String MANAGE_BLOG = "Gerenciador de Blogs";
	private static final String EXIT = "Sair";

	Command enter;
	Command save;
	Command exit;
	Command back;
	
	static List listBlogao;
	private static final int LIST_USER = 0;
	private static final int LIST_MANAGE_BLOG = 1;
	private static final int LIST_EXIT = 2;

	String user;
	private boolean logged;

	public Main() {
		user = "";
		logged = false;
		display = Display.getDisplay(this);
		display.setCurrent(form);
		midlet = this;
		Menu menu = new Menu();
		
		enter = new Command("Entrar", Command.ITEM, 0);
		save = new Command("Salvar", Command.ITEM, 0);
		exit = new Command("Sair", Command.BACK, 0);
		back = new Command("Voltar", Command.BACK, 0);
		
		listBlogao = new List("Blogao", List.IMPLICIT);
		listBlogao.append(MANAGE_BLOG, null);
		listBlogao.append(EXIT, null);
		listBlogao.addCommand(enter);
		listBlogao.addCommand(exit);
		listBlogao.setCommandListener(this);

		login();
	}

	public void commandAction(Command command, Displayable d) {
		if (command == enter){
			if (logged){
				switch (listBlogao.getSelectedIndex()) {
				case LIST_USER:
					information();
					break;
				case LIST_MANAGE_BLOG:
					menu.view();
					break;
				case LIST_EXIT:
					exitMIDlet();
					break;
				}
			} else{
				if (Login.validateLogin()){
					getDisplay().setCurrent(listBlogao);
					logged = true;
				}
            }
       }
       if (command == exit){
			if (getDisplay().getCurrent().equals(listBlogao))
				exitMIDlet();
		    }
		if (command == back){
			switch (listBlogao.getSelectedIndex()){
			case LIST_USER:
				getDisplay().setCurrent(listBlogao);
				break;
		    case LIST_MANAGE_BLOG:
		    	getDisplay().setCurrent(listBlogao);
		    	break;
		    default:
		    	break;
			}
		}
	}
	
	private void information() {
    	Form userInformation = new Form("Informacoes de " + user);
    	userInformation.addCommand(back);
    	userInformation.append(new StringItem("Nome:", user));
    	userInformation.append(new StringItem("E-mail:", user + "@gmail.com"));
    	userInformation.append(new StringItem("Sexo:", " "));
    	userInformation.append(new StringItem("Quem sou eu:", " "));
    	userInformation.setCommandListener(this);
    	getDisplay().setCurrent(userInformation);
    }

	public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void exitMIDlet() {
        display.setCurrent(null);
        notifyDestroyed();
    }

    public static Display getDisplay() {
    	return display;
    }

    public static List getListBlogao() {
    	return listBlogao;
    }

    private void login() {
    	Form loginWindow = Login.getForm();
    	loginWindow.addCommand(enter);
    	loginWindow.setCommandListener(this);
    	user = Login.getUserName().getString();
    	getDisplay().setCurrent(loginWindow);
    }

    public static void main(String[] args) {
    	new Main();
    }

	protected void pauseApp() {
		// TODO Auto-generated method stub
		
	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		
	}
}